package cn.iocoder.an.module.mold.controller.admin.upkeepstatics;

import com.fhs.core.trans.anno.TransMethodResult;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.an.framework.common.pojo.PageParam;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.pojo.CommonResult;
import cn.iocoder.an.framework.common.util.object.BeanUtils;
import static cn.iocoder.an.framework.common.pojo.CommonResult.success;

import cn.iocoder.an.framework.excel.core.util.ExcelUtils;

import cn.iocoder.an.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.an.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.an.module.mold.controller.admin.upkeepstatics.vo.*;
import cn.iocoder.an.module.mold.dal.dataobject.upkeepstatics.UpkeepStaticsDO;
import cn.iocoder.an.module.mold.service.upkeepstatics.UpkeepStaticsService;

@Tag(name = "管理后台 - 保养统计")
@RestController
@RequestMapping("/mold/upkeep-statics")
@Validated
public class UpkeepStaticsController {

    @Resource
    private UpkeepStaticsService upkeepStaticsService;

    @PostMapping("/create")
    @Operation(summary = "创建保养统计")
    @PreAuthorize("@ss.hasPermission('mold:upkeep-statics:create')")
    public CommonResult<Long> createUpkeepStatics(@Valid @RequestBody UpkeepStaticsSaveReqVO createReqVO) {
        return success(upkeepStaticsService.createUpkeepStatics(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新保养统计")
    @PreAuthorize("@ss.hasPermission('mold:upkeep-statics:update')")
    public CommonResult<Boolean> updateUpkeepStatics(@Valid @RequestBody UpkeepStaticsSaveReqVO updateReqVO) {
        upkeepStaticsService.updateUpkeepStatics(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除保养统计")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mold:upkeep-statics:delete')")
    public CommonResult<Boolean> deleteUpkeepStatics(@RequestParam("id") Long id) {
        upkeepStaticsService.deleteUpkeepStatics(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除保养统计")
                @PreAuthorize("@ss.hasPermission('mold:upkeep-statics:delete')")
    public CommonResult<Boolean> deleteUpkeepStaticsList(@RequestParam("ids") List<Long> ids) {
        upkeepStaticsService.deleteUpkeepStaticsListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得保养统计")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mold:upkeep-statics:query')")
    public CommonResult<UpkeepStaticsRespVO> getUpkeepStatics(@RequestParam("id") Long id) {
        UpkeepStaticsDO upkeepStatics = upkeepStaticsService.getUpkeepStatics(id);
        return success(BeanUtils.toBean(upkeepStatics, UpkeepStaticsRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得保养统计分页")
    @PreAuthorize("@ss.hasPermission('mold:upkeep-statics:query')")
    @TransMethodResult
    public CommonResult<PageResult<UpkeepStaticsRespVO>> getUpkeepStaticsPage(@Valid UpkeepStaticsPageReqVO pageReqVO) {
        PageResult<UpkeepStaticsDO> pageResult = upkeepStaticsService.getUpkeepStaticsPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, UpkeepStaticsRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出保养统计 Excel")
    @PreAuthorize("@ss.hasPermission('mold:upkeep-statics:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportUpkeepStaticsExcel(@Valid UpkeepStaticsPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<UpkeepStaticsDO> list = upkeepStaticsService.getUpkeepStaticsPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "保养统计.xls", "数据", UpkeepStaticsRespVO.class,
                        BeanUtils.toBean(list, UpkeepStaticsRespVO.class));
    }

}